/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "str_分割.h"
#include <字符串数值互换.h>
#include <regex>

//#include <boost/algorithm/string.hpp>


std::vector<std::wstring> f_str_分割(const std::wstring& 源, const std::wstring& 分隔符) {
	std::vector<std::wstring> tokens;
	size_t pos = 0;
	size_t delimiter_length = 分隔符.length();
	size_t prev = 0;

	while ((pos = 源.find(分隔符, prev)) != std::wstring::npos) {
		// 在分隔符前添加一个token
		if (pos != prev) {
			tokens.push_back(源.substr(prev, pos - prev));
		}
		prev = pos + delimiter_length;
	}

	// 添加最后一个token（如果有）
	if (prev != 源.length()) {
		tokens.push_back(源.substr(prev));
	}

	return tokens;

	//std::vector<std::wstring> 分割后文本;
	////boost::algorithm::split(分割后文本, 源, boost::algorithm::is_any_of(分隔符));
	////boost::algorithm::erase_all(tmpStrErase, "Hello");
	//for (std::vector<std::wstring>::iterator i = 分割后文本.begin(); i != 分割后文本.end(); ) {
	//	if ((*i).size()) ++i;
	//	else i = 分割后文本.erase(i);
	//}
	//
	//return 分割后文本;
}

std::vector<std::u16string> f_str_分割(const std::u16string& 源, const std::u16string& 分隔符) {
	std::vector<std::u16string> tokens;
	size_t pos = 0;
	size_t delimiter_length = 分隔符.length();
	size_t prev = 0;

	while ((pos = 源.find(分隔符, prev)) != std::wstring::npos) {
		// 在分隔符前添加一个token
		if (pos != prev) {
			tokens.push_back(源.substr(prev, pos - prev));
		}
		prev = pos + delimiter_length;
	}

	// 添加最后一个token（如果有）
	if (prev != 源.length()) {
		tokens.push_back(源.substr(prev));
	}

	return tokens;
}

std::vector<std::u16string> f_str_分割(const std::u16string& 源, const std::vector<std::u16string>& 分隔符) {
	std::vector<std::u16string> tokens;
	if(分隔符.empty()) return tokens;

	size_t pos = 0;
	size_t delimiter_length = 0;
	size_t prev = 0;

	for ( ; pos != std::wstring::npos; ) {
		
		for (auto& e : 分隔符) {
			pos = 源.find(e, prev);
			if (pos != std::wstring::npos) {
				delimiter_length = e.length();
				break;
			}
		}
		
		if (pos != prev) {
			tokens.push_back(源.substr(prev, pos - prev));
		}
		prev = pos + delimiter_length;
	}


	// 添加最后一个token（如果有）
	//if (prev != 源.length()) {
	//	tokens.push_back(源.substr(prev));
	//}

	return tokens;
}

void f_str_删除两头字符(std::wstring& 源, const std::wstring& 删除符) {
	源.erase(0, 源.find_first_not_of(删除符));
	源.erase(源.find_last_not_of(删除符) + 1);
}

void f_str_删除两头字符(std::wstring& 源, const std::wstring& 头, const std::wstring& 尾) {
	源.erase(0, 源.find_first_not_of(头));
	源.erase(源.find_last_not_of(尾) + 1);
}

ivec2 f_str_查找到字符区间(std::wstring& 源, const std::wstring& 查找符, const std::wstring& 终止符) {
	ivec2 r;
	auto posA = 源.find(查找符);
	auto posB = 源.find(终止符);

	if (posA != std::wstring::npos) {
		if (posB > posA) {
			r.x = posA;
		}
		else {
			r.x = -1;
		}
	}
	else {
		r.x = -1;
	}

	if (posA != std::wstring::npos) {
		r.y = posB;
	}
	else {
		r.y = -1;
	}
	
	return r;
}

std::u16string f_str_取到字符(std::u16string& 源, const std::u16string& 终止符) {
	auto posA = 源.find(终止符);
	std::u16string r;

	if (posA != std::u16string::npos) {
		r = 源.substr(0, posA);
		//源.erase(posA + 终止符.size());
	}

	return r;
}

wchar_t f_str_从跳过字符取字符(std::u16string& 源, const std::u16string& 跳过字符) {
	for (auto& e : 源) {
		bool 是否跳过 = false;
		for (auto k : 跳过字符) {
			if (k == e) {
				是否跳过 = true;
				break;
			}
		}

		if (是否跳过) {
			continue;
		}

		return e;
	}
	return 0;
}

void f_str_去除空白字符(std::u16string& s) {
	uint32 num = s.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= s.size()) break;
		if (s[i] == ' ') {
			s.erase(i, 1);
		}
		else {
			++i;
		}
	}
}

bool f_str_去路径空白字符(std::u16string& path) {
	uint32 num = path.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= path.size()) break;
		if (path[i] == ' ') {
			path.erase(i, 1);
		}
		else {
			++i;
		}
	}
	return false;
}



void f_str_数组填充末尾(const std::set<std::u16string>& key, std::u16string& s) {
	//std::wstring t = s;
	uint32 num = s.size();
	if (key.find(s) != key.end()) {
		for (uint32 i = 0; i < num; ++i) {
			auto t = s.back();
			if (t < '0' || t > '9' || s.size() <= 1) {
				break;
			}
			else {
				s.pop_back();
			}
		}

		if (key.find(s) == key.end()) {
			return;
		}

		for (uint32 i = 0; i < 256; ++i) {
			auto t = f_整数值转宽字符串(i);
			if (key.find(t + s) == key.end()) {
				s += t;
				break;
			}
		}
	}
	
}


// 检查位置是否为单词边界
Inline bool isWordBoundary(const std::string& str, size_t pos) {
	// 位置在字符串开头，或者前一个字符是非字母数字
	if (pos == 0 || !isalnum(str[pos - 1])) {
		return true;
	}
	return false;
}

int32 f_str_find_词最后出现位置(const std::string& str, const std::string& word, bool 全字匹配) {
	if (word.empty()) return -1;

	size_t pos = str.size();
	size_t wordLen = word.length();


	while (pos >= wordLen) {
		pos = str.find_last_of(word[0], pos - wordLen);

		if (pos == std::string::npos) break;

		// 检查是否匹配完整单词
		if (str.substr(pos, wordLen) == word) {
			if(!全字匹配) return pos;
			// 检查单词边界（前边界）
			if ( (pos == 0 || !isalnum(str[pos - 1])) ) {
				// 检查后边界
				size_t endPos = pos + wordLen;
				if (endPos == str.size() || !isalnum(str[endPos])) {
					return pos;
				}
			}
		}
	}

	return -1;
}

void f_str_去除指定词(std::string& str, const std::string& 词) {
	uint32 词数量 = 词.size();
	uint32 pos = 0;
	for (; pos < str.size(); ) {
		pos = str.find(词, pos);
		if(pos >= str.size()) break;

		str.erase(pos, 词数量);
		//pos += 词数量;
	}
}



//std::vector<std::wstring_view> f_strView_分割(const std::wstring& str, wchar_t delimiter) {
//	std::vector<std::wstring_view> result;
//	std::wstring_view sv(str);
//	size_t start = 0;
//	for (size_t i = 0; i <= sv.size(); ++i) {
//		if (i == sv.size() || sv[i] == delimiter) {
//			result.emplace_back(sv.substr(start, i - start));
//			start = i + 1;
//		}
//	}
//	return result;
//}



